﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SalesSys.Comm.Utility;
using SalesSys.DAL.Base;

namespace SalesSys.DAL.Models
{

    public abstract class PurMapper<T, TID> : BaseMap<T, TID> where T : DbEntityBase<TID>
    {
        public override string TablePrefix
        {
            get { return EntityTabConfig.PurchaseTabPrefix; }
        }
    }
  
    /// <summary>
    /// 采购单来源类型
    /// </summary>
    public enum PurchaseType
    {
        /// <summary>
        /// 直接创建
        /// </summary>
       [Description("直接创建")]
        Creater=0,
        /// <summary>
        /// 销售单创建
        /// </summary>
        [Description("销售单创建")]
        Sale
    }

    /// <summary>
    /// 采购商品信息实体类
    /// </summary>
    public class PurchaseGoods : DbEntityBase<Guid>
    {
      
        public class Mapper : PurMapper<PurchaseGoods, Guid>
        {
            public Mapper()
            {//todo 记录应删除，但提示循环
                // HasMany(p => p.Records).WithOptional().WillCascadeOnDelete();
            }
        }

        /// <summary>
        /// 商品
        /// </summary>
        public virtual Commodity Commodity { get; set; }

        /// <summary>
        /// 数量
        /// </summary>
        public int Count { get; set; }

        /// <summary>
        /// 价格
        /// </summary>
        public decimal Price { get; set; }
        /// <summary>
        /// 规范价，统一使用的非标价,算法：标件价-非标优惠
        /// </summary>
        public decimal NormPrice { get; set; }
        /// <summary>
        /// 总重,应收
        /// </summary>
        [DecimalPrecision(18, 3)]
        public decimal Weight { get; set; }

        /// <summary>
        /// 原始价格
        /// </summary>
        public decimal SourcePrice { get; set; }
        /// <summary>
        /// 预估
        /// </summary>
        [DecimalPrecision(18, 3)]
        public decimal SourceWeight { get; set; }

        /// <summary>
        /// 历史记录
        /// </summary>
        public virtual ICollection<PurchaseRecord> Records { get; set; }
        /// <summary>
        /// 所属订单
        /// </summary>
        public virtual PurchaseOrder Order { get; set; }
    }
    /// <summary>
    /// 采购单
    /// </summary>
    public class PurchaseOrder : DbEntityBase<Guid>
    {
        public PurchaseOrder()
        {
            CreateTime = DateTime.Now;
        }

        public static void AddPurchaseRecord(IEmployee handler, PurchaseOrder order, PurchaseRecordType type, string msg = null)
        {

            var record = new PurchaseRecord();
            if (handler != null)
            {
                record.HandlerName = handler.Name;
                record.HandlerId = handler.Id;
            }
            record.LinkId = order.Id + "";
            record.Type = type;
            record.Remarks = msg + ";";
            switch (type)
            {
                case PurchaseRecordType.Msg:

                    break;
                case PurchaseRecordType.Edit:
                    break;
                case PurchaseRecordType.StateChange:
                    record.Tag = order.State + "";
                    record.Remarks += "订单状态变为：" + order.State.GetDescInfo();
                    break;
                case PurchaseRecordType.ApprovalChange:
                    break;
                default:
                    throw new ArgumentOutOfRangeException("type");
            }


            order.Records.Add(record);
        }

        public class Mapper : PurMapper<PurchaseOrder, Guid>
        {
            public Mapper()
            {
                HasMany(p => p.Records).WithOptional().WillCascadeOnDelete();
                HasMany(p => p.Goodses).WithOptional().WillCascadeOnDelete();
                //HasMany(p => p.VehicleInfos).WithOptional().WillCascadeOnDelete();
            }
        }

        /// <summary>
        /// 创建人，一般为客户专员
        /// </summary>
        public virtual Employee Creater { get; set; }

        /// <summary>
        /// 支付户头
        /// </summary>
        [Obsolete("不再使用")]
        public virtual string Account { get; set; }
        /// <summary>
        /// 货款是否标件
        /// </summary>
        public bool IsStandard { get; set; }
        /// <summary>
        /// 所在地区
        /// </summary>
        public virtual District District { get; set; }

        ///// <summary>
        ///// 详细地址
        ///// </summary>
        //public string Address { get; set; }


        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 运费户头
        /// </summary>
        [Obsolete("不再使用")]
        public virtual string DeliveryAccount { get; set; }


        /// <summary>
        /// 每吨运费
        /// </summary>
        public decimal DeliveryPrice { get; set; }

        /// <summary>
        /// 总运费，
        /// </summary>
        public decimal DeliveryPriceTotal { get; set; }

        /// <summary>
        /// 运费是否公户,//todo 对于直发采购运费信息如何确定
        /// </summary>
        public bool IsPublicDelivery { get; set; }

        /// <summary>
        /// 联系人
        /// </summary>
        public string Contact { get; set; }

        ///// <summary>
        ///// 联系人手机号
        ///// </summary>
        //public string ContactPhone { get; set; }

        ///// <summary>
        ///// 车辆信息
        ///// </summary>
        //public string VehicleInfos { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string Remarks { get; set; }

        /// <summary>
        /// 供应商
        /// </summary>
        public virtual Provider Provider { get; set; }

        /// <summary>
        /// 货品信息
        /// </summary>
        public virtual ICollection<PurchaseGoods> Goodses { get; set; }

        /// <summary>
        /// 订单类型,直接创建，销售单创建
        /// </summary>
        public PurchaseType PurchaseType { get; set; }

        /// <summary>
        /// 来源id，销售单Id
        /// </summary>
        public Guid? FromId { get; set; }
        /// <summary>
        /// 关联入库,直发关联客户
        /// </summary>
        public Guid? ToId { get; set; }
        /// <summary>
        ///总重量
        /// </summary>
        [DecimalPrecision(18, 3)]
        public decimal TotalWeight { get; set; }

        /// <summary>
        /// 实际价格
        /// </summary>
        public decimal Total { get; set; }

        /// <summary>
        /// 退款
        /// </summary>
        public decimal Refund { get; set; }

        /// <summary>
        /// 原重量
        /// </summary>
        [DecimalPrecision(18, 3)]
        public decimal SourceWeight { get; set; }

        /// <summary>
        /// 原价格
        /// </summary>
        public decimal SourceTotal { get; set; }

        /// <summary>
        /// 订单状态
        /// </summary>
 
        public PurchaseState State { get; set; }
        /// <summary>
        /// 收货仓库
        /// </summary>
        public virtual Provider ToDepot { get; set; }
       
        public enum PurchaseState
        {
            /// <summary>
            /// 待确认，可以编辑价格
            /// </summary>
            [Description("待确认")] Pending = 0,

            /// <summary>
            /// 审批中
            /// </summary>
            [Description("审批中")]
            Approval = 1,

            /// <summary>
            /// 采购中
            /// </summary>
            /// <remarks>
            /// 可以修改实际重量价格
            /// </remarks>
            [Description("采购中")]
            Purchasing = 2,
                /// <summary>
            /// 取消
            /// </summary>
            [Description("取消")]
            Cancel =3,
            /// <summary>
            /// 完成采购
            /// </summary>
            /// <remarks>
            /// 完成采购后不允许修改采购单
            /// </remarks>
            [Description("完成采购")]
            Complete=4,
            [Description("入库审批")]
            ApplyEntry=5,
            [Description("已入库")]
            EntryEnd=6,
            /// <summary>
            /// 用于直发采购标记
            /// </summary>
            [Description("已发货")]
           Send=7,
            /// <summary>
            /// 已完结，不允许进行任何修改操作
            /// </summary>
            [Description("已完结")]
            End=8
        }

        ///// <summary>
        ///// 是否已结算
        ///// </summary>
        //public bool IsSettlement { get; set; }

        ///// <summary>
        ///// 结算时间
        ///// </summary>
        //public DateTime? SettlementTime { get; set; }

        /// <summary>
        /// 是否暂停
        /// </summary>
        public bool IsPause { get; set; }

       


        /// <summary>
        /// 订单变化记录
        /// </summary>
        public virtual ICollection<PurchaseRecord> Records { get; set; }


        /// <summary>
        /// 根据商品信息重新计算重量及价格
        /// </summary>
        public void CalcNew()
        {
            if (Goodses == null)
            {
                TotalWeight = 0;
                Total = 0;
                SourceWeight = 0;
                SourceTotal = 0;
            }
            else
            {
                TotalWeight = Goodses.Sum(p => p.Weight);
                Total = Goodses.Sum(p => p.Price * p.Weight);
                if (SourceTotal == 0)
                    SourceTotal = Goodses.Sum(p => p.Price * p.Weight);
                if (SourceWeight == 0)
                    SourceWeight = TotalWeight;
            }
        }
    }

    public enum PurchaseRecordType
    {
        Msg=0,
        Edit=1,
        StateChange,
        ApprovalChange
    }
    /// <summary>
    /// 订单变化记录
    /// </summary>
    public class PurchaseRecord : DbEntityBase<Guid>
    {
        public PurchaseRecord()
        {
            CreateTime = DateTime.Now;
        }

        public class Mapper : PurMapper<PurchaseRecord, Guid>
        {
        }

        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 记录类型
        /// </summary>
        public PurchaseRecordType Type { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string Remarks { get; set; }

        /// <summary>
        /// 关联人名称
        /// </summary>
        public string HandlerName { get; set; }

        /// <summary>
        /// 关联人编号
        /// </summary>
        public Guid? HandlerId { get; set; }
        /// <summary>
        /// 相关数据id
        /// </summary>
        public string LinkId { get; set; }

        /// <summary>
        /// 自定义数据标记
        /// </summary>
        public string Tag { get; set; }
    }
}

